package ar.Location.Map;

import java.util.List;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import ar.Location.Api.GooglePlace.GooglePlace;
import ar.Location.Constant.Define;
import ar.Location.PlaceObject.Place;

import com.google.android.maps.GeoPoint;

public class MyGeocoder {

	private static Context geocoderContext = null;
	private static Geocoder geocoder = null;

	public static Geocoder getGeoCoderInstance(Context context) {
		if (geocoder == null) {
			geocoderContext = context;
			geocoder = new Geocoder(geocoderContext);
		}
		return geocoder;
	}

	public MyGeocoder(Context context) {
		geocoderContext = context;
		if (geocoder == null) {
			geocoder = new Geocoder(geocoderContext);
		}
	}

	public MyOverlayItem search(String locationName) {
		List<Address> listResults = getListResults(locationName, 1);
		return getFirstResult(listResults);
	}

	public MyOverlayItem search(GeoPoint location) {
		List<Address> listResults = getListResults(location, 1);
		return getFirstResult(listResults);
	}

	public List<Address> getListResults(String locationName,
			int maxNumberOfResutls) {
		if (geocoder == null) {
			return null;
		} else {
			try {
				List<Address> listAddresses = geocoder.getFromLocationName(
						locationName, maxNumberOfResutls);
				if (listAddresses != null && listAddresses.size() > 0) {
					return listAddresses;
				} else {
					return null;
				}
			} catch (Exception e) {
				// TODO: handle exception
				return null;
			}
		}
	}

	public List<Address> getListResults(GeoPoint location,
			int maxNumberOfResutls) {
		if (geocoder == null) {
			return null;
		} else {
			try {
				List<Address> listAddresses = geocoder.getFromLocation(
						location.getLatitudeE6() / 1E6,
						location.getLongitudeE6() / 1E6, maxNumberOfResutls);
				if (listAddresses != null && listAddresses.size() > 0) {
					return listAddresses;
				} else {
					return null;
				}
			} catch (Exception e) {
				// TODO: handle exception
				return null;
			}
		}
	}

	public String getFirstAddress(String latitude, String longitude) {
		float lat = Float.parseFloat(latitude);
		float lng = Float.parseFloat(longitude);
		int mylat = (int) (lat * 1E6);
		int myLng = (int) (lng * 1E6);
		GeoPoint geoPoint = new GeoPoint(mylat, myLng);
		List<Address> listAddresses = getListResults(geoPoint, 1);
		if (listAddresses != null && listAddresses.size() > 0) {
			return listAddresses.get(0).getAddressLine(0);
		}
		return null;
	}

	private MyOverlayItem getFirstResult(List<Address> listResults) {
		// TODO Auto-generated method stub
		if (listResults != null) {
			Address result = listResults.get(0);
			if (result != null) {
				float latitude = (float)(result.getLatitude());
				float longitude = (float)(result.getLongitude());
				
				int intergerLat = (int) (latitude * 1E6);
				int intergerLng = (int) (longitude * 1E6);
				GeoPoint geoPoint = new GeoPoint(intergerLat, intergerLng);

				String title = result.getAddressLine(0);
				
				String adminArea = result.getAdminArea();
				String countryName = result.getCountryName();
				String snippet = title + " " + adminArea + " " + countryName;
				
				MyOverlayItem overlayResult = new MyOverlayItem(geoPoint,
						title, snippet);
				
				Place place = new GooglePlace(Define.DS_GOOGLE_PLACE);
				place.name = title;
				place.address = snippet;
				place.latitude = String.valueOf(latitude);
				place.longitude = String.valueOf(longitude);
				
				overlayResult.updateInfoFrom(place);
				
				return overlayResult;
			}
		}
		return null;
	}

}
